import ORIGIN from '../../assets/json/origin.json';
import PC from '../../assets/json/pc-code.json';
export const addFormMixin={
    data(){
        return{
            			//授权表单
			auth:{
				phone:'',
				code:''
            },
            vipUpdateFlag:false,//true为更新会员信息，false为新增
			memberDetail:'',//查看会员详情的信息
			ORIGIN,//省市区
			PC,//省市
			// addFormVisible: false,//新增界面是否显示
			addFormRules: {
				name: [
					{ required: true, message: '请输入姓名', trigger: 'blur' }
				],
				phone: [
					{ required: true, message: '请输入手机号', trigger: 'blur' }
				],
				// sex: [
				// 	{ required: true, message: '请选择性别', trigger: 'blur' }
				// ],
				age: [
					{ required: true, message: '请输入年龄', trigger: 'blur' }
				],
				level: [
					{ required: true, message: '请输入会员等级', trigger: 'blur' }
				],
				origin: [
					{ required: true, message: '请输入籍贯', trigger: 'blur' }
				],
				admissionTime: [
					{ required: true, message: '请输入入会时间', trigger: 'blur' }
				],
				unit: [
					{ required: true, message: '请输入单位名称', trigger: 'blur' }
				],
				address: [
					{ required: true, message: '请输入单位地址', trigger: 'blur' }
				],
				// expires: [
				// 	{ required: true, message: '请输入到期时间', trigger: 'blur' }
				// ]
			},
			authFormRules:{
				phone: [
					{ required: true, message: '请输入手机号', trigger: 'blur' }
				]
			},
			payModeFlag:true,//能否选择缴费方式
			phoneList:[],//新增会员的搜索手机列表
			loading:false,//搜索手机列表的loading
			//新增界面数据
			addForm: {
                noPrint:'',//表单不能进行标记
                is_vip:'',//是否已经为vip，1只能编辑不能新增
				number:'',//会员编号
                realname: '',
                status:'',//激活状态
				phone:'',
				sex: '',
				age: '',
				payMode:'',//缴费方式
				level: '',
				origin:'',//籍贯
				admissionTime:'',//入会时间
				unit:'',
				address: '',
				addressdetail:'',//公司详细地址
				cost:'',
				expires:'',//到期时间
			},
			//会员级别
			levelOptions: [],
			//会费标准
			costOptions: [
				{
					value: '25万/届',
					label: '25万/届'
				},
				{
					value: '15万/届',
					label: '15万/届'
				},
				{
					value: '10万/届',
					label: '10万/届'
				},
				{
					value: '5万/届',
					label: '5万/届'
				},
				{
					value: '1.5万/届',
					label: '1.5万/届'
				},
				{
					value: '1千/年',
					label: '1千/年'
				}
			],
			//会员缴费方式
			payModeOptions:[
                {label:'免费',value:0,disabled:false},
				{label:'一次性',value:1,disabled:false},
				{label:'分期',value:2,disabled:false}
            ],
            vipList:[],
			id:'',
			vipId:'',//修改会员时传入
        }
    },
    watch:{
		addForm:{
			handler:function(nval){
                
				if(nval.level){
					this.payModeFlag=false;//先选中会员等级才能选缴费方式
				}else{
					this.payModeFlag=true;
                }
			},
			deep:true
        },
    },
    methods: {
        async searchphone(phone,cb){
            const data=await this.$api.getVipSearchUserInfo({
                phone:phone
            })
            if(!phone){
                //空的情况不查询
                return
            }
            if(data.data.level=='success'){
                this.phoneList=data.data.data.user_vip_list;
                const list=data.data.data.user_vip_list;
                const arr=[]
                if(list.length==0){
                    cb([])
                    return
                }
                list.forEach((val,index)=>{
                    let obj={};
                    if(val.realname){
                        obj.label=`姓名：${val.realname} 电话：${val.phone}`;
                    }else{
                        obj.label=`电话：${val.phone}`;
                    }
                    
                    obj.value=val.phone;
                    obj.index=index
                    arr.push(obj)
                })

                cb([...arr])
            }
        },
        handleSelect(e){
            //选中手机号,表格中填默认值
            
            const index=e.index;
            const memberDetail=this.phoneList[index];

            this.addForm.is_vip=memberDetail.is_vip;

            if(this.addForm.is_vip==1){
                this.$message({
                    type:'error',
                    message:'该用户已存在'
                })
                // this.addForm.noPrint=true;
                // this.$set(this.addForm,'noPrint',true)
                // return 
            }
            this.addForm.realname=memberDetail.realname;
            this.addForm.status=+memberDetail.status;
            this.addForm.phone=memberDetail.phone;
            this.addForm.sex=+ memberDetail.sex;
            this.addForm.age=memberDetail.age;
            
            this.addForm.level=memberDetail.vip_code;
            setTimeout(()=>{
                this.addForm.payMode= memberDetail.pay_mode;
            },500)
            this.addForm.origin=[memberDetail.home_province,memberDetail.home_city,memberDetail.home_area]
            this.addForm.admissionTime=memberDetail.start_time;
            this.addForm.unit=memberDetail.company_name;
            this.addForm.address=[memberDetail.province,memberDetail.city,memberDetail.area];
            this.addForm.addressdetail=memberDetail.company_address;
            this.id=memberDetail.id;
            this.vipId=memberDetail.vipId;

            
            // this.vipUpdateFlag=true;

            if(this.addForm.level){
                this.levelChange(this.addForm.level);//更新缴费方式的可选项
            }
        },
        async getUserVipRule(){
            //获取会员等级&缴费规则接口
            const data=await this.$api.getUserVipRule();
            if(data.data.level=='success'){
                this.levelOptions= data.data.data.vip_list
            }
        },
        
        getUserVipUpdate(id,vipId){
            //修改会员信息
            this.dialogTit="管理会员信息"
            this.vipUpdateFlag=true;//区分新增还是修改会员
            this.id=id;
            this.vipId=vipId;

            // /*注释掉授权弹窗/*
            this.authSubmit()
            // this.authVisible=true;

        },
        closeDialog(){
            //重置数据
            this.id='';
            this.vipId=''
            this.addForm= {
                is_vip:'',
				number:'',//会员编号
                realname: '',
                status:'',//激活状态
				phone:'',
				sex: '',
				age: '',
				payMode:'',//缴费方式
				level: '',
				origin:'',//籍贯
				admissionTime:'',//入会时间
				unit:'',
				address: '',
				addressdetail:'',//公司详细地址
				cost:'',
				expires:'',//到期时间
			};
        },
        async authSubmit(){
            //提交授权按钮
            this.authVisible=false;
            this.addFormVisible=true;
            const data=await this.$api.getUserVipDetail({
                id:this.id,
                vipId:this.vipId
            });
            if(data.data.level=='success'){
                let memberDetail=data.data.data.user_vip_info;
                this.addForm.realname=memberDetail.realname;
                this.addForm.status=memberDetail.status;
                this.addForm.phone=memberDetail.phone;
                this.addForm.sex=+ memberDetail.sex;
                this.addForm.age=memberDetail.age;
                
                this.addForm.level=memberDetail.vip_code;
                this.addForm.payMode=memberDetail.pay_mode;
                this.addForm.origin=[memberDetail.home_province,memberDetail.home_city,memberDetail.home_area]
                this.addForm.admissionTime=memberDetail.start_time;
                this.addForm.unit=memberDetail.company_name;
                this.addForm.address=[memberDetail.province,memberDetail.city,memberDetail.area];
                this.addForm.addressdetail=memberDetail.company_address;

            }
        },
        submitForm(formName) {
            //提交表单验证
             
            this.$refs[formName].validate((valid) => {
                // if(true){
            if (valid) {
                if(this.vipUpdateFlag){
                    this.getUserVipUpdateHandle(this.id,this.vipId);//更新会员信息
                }else{
                    this.getUserVipInsertHandle();//新增会员
                }
                
                
            } else {
                console.log('error submit!!');
                return false;
            }
            });
          },
          levelChange(e){
               //选中会员等级，更改相应缴费方式
            this.addForm.payMode='';//重置缴费方式
            this.payModeOptions=[
                {label:'一次性',value:'1',disabled:false},
                {label:'分期',value:'2',disabled:false}
            ];
            const rule=this.levelOptions.find(val=>{
                return val.code==e
            })
            const list=rule.pay_rule_list;
            const listmatch=list.map(item=>{
                return item.type
            })
            this.payModeOptions.forEach((val,index,arr)=>{
                if(!listmatch.includes(val.value)){
                    let item=val;
                    item.disabled=true;
                    this.$set(this.payModeOptions,index,item)
                }
            })
          },
          async getUserVipInsertHandle(){
              //新增会员信息
              const msg=this.addForm;
                  const thsAreaCode = this.$refs['origin'].getCheckedNodes() ;
                	const code=thsAreaCode[0].data.value ||'';//获取地区编码
                const req=await this.$api.getUserVipInsert({
                    realname:msg.realname,
                    phone:msg.phone,
                    age:msg.age,
                    companyName:msg.unit,
                    companyAddress:msg.addressdetail,
                    province:msg.address[0],
                    city:msg.address[1],
                    area:msg.address[2],
                    vipCode:msg.level,//会员等级
                    payMode:msg.payMode,//1全款 2分期
                    areaCode:code,
                    sex:msg.sex,
                    homeProvince:msg.origin[0],
                    homeCity:msg.origin[1],
                    homeArea:msg.origin[2],
                    startTime:msg.admissionTime,

                })
                if(req.data.level=='success'){
                    this.$message({
                        message: req.data.message,
                        type: 'success'
                        });
                        this.getUserVipList();
                    this.addFormVisible=false;
                }else{
                    this.$message({
                    message: req.data.message,
                    type: 'error'
                    });
                }
          },
          async getUserVipUpdateHandle(id,vipId){
            //更新会员信息
            const msg=this.addForm;
                const thsAreaCode = this.$refs['origin'].getCheckedNodes() ;
              	const code=thsAreaCode[0].data.value ||'';//获取地区编码
              const req=await this.$api.getUserVipUpdate({
                  realname:msg.realname,
                  phone:msg.phone,
                  age:msg.age,
                  companyName:msg.unit,
                  companyAddress:msg.addressdetail,
                  province:msg.address[0],
                  city:msg.address[1],
                  area:msg.address[2],
                  vipCode:msg.level,//会员等级
                  payMode:msg.payMode,//1全款 2分期
                  areaCode:code,
                  sex:msg.sex,
                  homeProvince:msg.origin[0],
                  homeCity:msg.origin[1],
                  startTime:msg.admissionTime,
                  id:id,
                  vipId:vipId
              })
              if(req.data.level=='success'){
                  this.$message({
                      message: req.data.message,
                      type: 'success'
                      });
                      this.getUserVipList();
                  this.addFormVisible=false;
              }else{
                  this.$message({
                  message: req.data.message,
                  type: 'error'
                  });
              }
        },
    },
    mounted() {
        this.getUserVipRule()
    },
}